# 一、载入名人照片数据集
from sklearn.datasets import fetch_lfw_people

import matplotlib.pyplot as plt
data_home="D:\\git code\\xiangmu\\kecheng\\ml-lesson\\03_dataset\\item7"     #可替换路径
faces=fetch_lfw_people(min_faces_per_person=60,data_home=data_home)

x,y=faces.data,faces.target
target_names=faces.target_names
n_samples,h,w=faces.images.shape

print(target_names)	
print(n_samples,h,w)



# 二、数据降维处理
from sklearn.decomposition import PCA     #降维算法

#降维处理，将维度降到15个
n_components=150
pca=PCA(n_components=n_components,svd_solver='randomized',whiten=True,random_state=70).fit(x)
eigenfaces=pca.components_.reshape((n_components,h,w))        #提取特征值
x_pca=pca.transform(x)	                                      #将训练集转化成低维度的特征向量


fig,ax=plt.subplots(3,5)
for i,axi in enumerate(ax.flat):
    axi.imshow(eigenfaces[i].reshape(h,w),cmap='bone')
    axi.set(xticks=[],yticks=[])
plt.show()



# 三、分割数据集、寻找最优值、输出评估报告
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV 
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
import numpy as np

#分割数据集
x_train,x_test,y_train,y_test=train_test_split(x_pca,y,test_size=400,random_state=42)

#网格搜索最优值
param_grid={'C':[1,5,10,50,100],'gamma':[0.0001,0.0005,0.001,0.005,0.01,0.1]}
grid=GridSearchCV(SVC(kernel="rbf",random_state=0),
param_grid=param_grid,cv=5)
grid.fit(x_train,y_train)

#获取最优模型
print(f"最优参数值为：{grid.best_params_}")
model=grid.best_estimator_
pred=model.predict(x_test)
re = classification_report(y_test,pred,target_names=faces.target_names)
print("最优模型的评估报告：",re)



#  四、显示分类结果
fig,ax=plt.subplots(4,6)
for i,axi in enumerate(ax.flat):
    axi.imshow(eigenfaces[i].reshape(h,w),cmap='bone')#绘制图像
    axi.set(xticks=[],yticks=[])
    box=dict(fc='red',alpha=0.4)#设置边框样式
    axi.set_ylabel(faces.target_names[pred[i]].split()[-1],bbox=None if pred[i]==y_test[i] else box)#显示预测姓名

plt.rcParams['font.sans-serif']='Simhei'
plt.suptitle('预测名人的姓名（加边框的名字表示预测错误）',size=10)
plt.show()